/**
 * 访问"蛋糕"接口网络模块
 */
import axios from "./axios.config";
import util from "./util";

class CakeApi {
  async findAll(limit, offset, orderBy, sort) {
    let result = await axios.get(
      `/cakes?limit=${limit}&offset=${offset}&order_by=${orderBy}&sort=${sort}`
    );
    return util.jsonTransfer(result.data);
  }

  async getCount() {
    let result = await axios.get(`/cakes/count`);
    return util.jsonTransfer(result.data);
  }

  async find(cakeId) {
    let result = await axios.get(`/cakes/${cakeId}`);
    return util.jsonTransfer(result.data);
  }

  async fetchDouban(isbn) {
    let result = await axios.get(`/cakes/fetch/douban/${isbn}`);
    return util.jsonTransfer(result.data);
  }

  async add(cake) {
    let result = await axios.post(`/cakes`, cake);
    return util.jsonTransfer(result.data);
  }

  async edit(cake) {
    let result = await axios.put(`/cakes/${cake.id}`, cake);
    return util.jsonTransfer(result.data);
  }

  async delete(cakeId) {
    let result = await axios.delete(`/cakes/${cakeId}`);
    return util.jsonTransfer(result.data);
  }

  async search(keyword) {
    let result = await axios.get(`/cakes/search?q=${keyword}`);
    return util.jsonTransfer(result.data);
  }

  async findByIsn(isbn) {
    let result = await axios.get(`/cakes/isbn/${isbn}`);
    return util.jsonTransfer(result.data);
  }
}

export default CakeApi;
